<?php

namespace humhub\modules\api\controllers;

use Yii;
use yii\web\UnauthorizedHttpException;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
use humhub\modules\user\widgets\UserListBox;
use humhub\modules\space\models\Setting;

use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
#use yii\filters\auth\HttpBasicAuth;
#use yii\filters\auth\QueryParamAuth;

/**
 * 获取指定用户Post内容列表, 单条内容, 我的任务，我的课程...
 */
class PostController extends Base2Controller
{

    /**
     * @inheritdoc
     */
    public function init(){
        $_GET['uguid'] = Yii::$app->request->get('u');
        //$id = (int)Yii::$app->request->get('u');
        //$user = User::findOne(['guid' => $id]);
        //if( $user ){
        //    $_GET['uguid'] = $user->guid;
        //}else{
        //   throw new \yii\web\BadRequestHttpException('User not found');
        //}
        parent::init();
    }

    /**
     * @inheritdoc
     */
    public function behaviors(){
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => CompositeAuth::className(),
            'authMethods' => [
                //QueryParamAuth::className(),
                HttpBearerAuth::className(),
            ],
        ];

        $behaviors['acl'] = [
            'class' => \humhub\components\behaviors\AccessControl::className(),
            'guestAllowedActions' => ['stream']
        ];

        return $behaviors;
    }

    /**
     * @inheritdoc
     */
    public function actions(){
        $actions = parent::actions();
        unset($actions['update'], $actions['create'], $actions['index'], $actions['delete']);
        return $actions;

        return array(
            'stream' => array(
                'class' => \humhub\modules\api\cccstream\ContentContainerStream::className(),
                'mode'  => \humhub\modules\api\cccstream\ContentContainerStream::MODE_NORMAL,
                'contentContainer' => $this->contentContainer
            ),
        );
    }

    /**
     * 获取指定用户Post内容列表
     *
     * GET api/post/stream?u=[:guid]&limit=[4]&filters=&sort=c&from=[66]&mode=normal
     *
     * get.u 用户的guid
     *
     * get.p_type : 类型筛选: 空=全部, 0=图文, 1=任务, 2=课程, 3=视频
     *
     * get.limit : 单次获取总量
     *
     * get.from : 起始wallEntryID，默认为空''=第一页
     *
     * @return array  output:返回的内容, counter:表示获取几个wallEntry,  entryIds:表示所有返回的wallEntryID, lastEntryId:列表中最后一个EntryId
     */
    public function actionStream(){
        $stream = Yii::createObject([
            'class' => \humhub\modules\api\cccstream\ContentContainerStream2::className(),
            'mode'  => \humhub\modules\api\cccstream\ContentContainerStream2::MODE_NORMAL,
            'contentContainer' => $this->contentContainer
        ], ['stream', $this]);
        return $stream->run();
    }

    /**
     * 获取用户自己的Post内容列表(可以用于获取: 我的任务，我的课程...)
     *
     * GET api/post/stream3me?u=[:guid]&limit=[4]&filters=&sort=c&from=[66]&mode=normal
     *
     * 传参、返回参考:/api/post/stream 其中可根据 p_type 属性获取：我的任务，我的课程... p1type支持任务状态筛选(进行中...）
     */
    public function actionStream3me(){
        $stream = Yii::createObject([
            'class' => \humhub\modules\api\cccstream\ContentContainerStream3::className(),
            'mode'  => \humhub\modules\api\cccstream\ContentContainerStream3::MODE_NORMAL,
            'contentContainer' => $this->contentContainer
        ], ['stream', $this]);
        return $stream->run();
    }

    /**
     * 获取单条Post内容(仅限纯图文内容,属于Space的内容请参考Space下的方法，如果是任务或课程请查看是否有特定接口)
     *
     * GET api/post/[:wallentryId]
     *
     * :wallentryId = 列表中返回的 wallentryid
     *
     * get.u Post所属用户的guid
     *
     * @return array  返回的结构同列表
     */
    public function actionView(){
        //基于 Stream 的方式
        if( $_GET['id'] ){
            $_GET['id_wallentryId'] = $_GET['id'];
        }else{
            throw new \yii\web\NotFoundHttpException('Not Found');
        }
        $stream = Yii::createObject([
            'class' => \humhub\modules\api\cccstream\ContentContainerStream::className(),
            'mode'  => \humhub\modules\api\cccstream\ContentContainerStream::MODE_NORMAL,
            'contentContainer' => $this->contentContainer
        ], ['stream', $this]);
        return $stream->run();
    }
}
?>
